/* 
 *  Syntelos XB
 *  Copyright (C) 2006 John Pritchard.
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 *  02111-1307 USA
 */

package syntelos.xb;

/** 
 * <p> An element subclassed by {@link Type$Descriptor}, {@link
 * Type$Class} and {@link Class}.  </p>
 * 
 * @author John Pritchard (jdp@syntelos.com)
 */
public abstract class Type 
    extends Base
{
    public final static syntelos.iou.dom.Name NAME = new syntelos.iou.dom.impl.Name(ELEMENT_NODE,"http://www.syntelos.org/xb","xb:type");


    /**
     * <p> Construct a native type descriptor for a "reflected"
     * class. </p>
     * 
     * @param jclass An existing class
     * @return A native (JVM) type descriptor for the argument class
     */
    public final static java.lang.String Type4(java.lang.Class jclass){
        if (null == jclass)
            return null;
        else if (java.lang.Void.TYPE == jclass)
            return "V";
        else if (jclass.isPrimitive()){
            if (java.lang.Boolean.TYPE == jclass)
                return "Z";
            else if (java.lang.Byte.TYPE == jclass)
                return "B";
            else if (java.lang.Short.TYPE == jclass)
                return "S";
            else if (java.lang.Character.TYPE == jclass)
                return "C";
            else if (java.lang.Integer.TYPE == jclass)
                return "I";
            else if (java.lang.Long.TYPE == jclass)
                return "J";
            else if (java.lang.Float.TYPE == jclass)
                return "F";
            else if (java.lang.Double.TYPE == jclass)
                return "D";
            else
                throw new syntelos.xb.Error.Bug(jclass.toString());
        }
        else if (jclass.isArray())
            return (jclass.getName().replace('.','/'));
        else {
            syntelos.iou.chbuf strbuf = new syntelos.iou.chbuf();
            strbuf.append('L');
            strbuf.append(jclass.getName().replace('.','/'));
            strbuf.append(';');
            return strbuf.toString();
        }
    }

    /**
     * <p> Construct a native type descriptor for a reflected
     * method. </p>
     * 
     * @param jmethod An existing method
     * @return A native (JVM) type descriptor for the argument method
     */
    public final static java.lang.String Type4(java.lang.reflect.Method jmethod){
        if (null == jmethod)
            throw new Error.Argument("Null argument.");
        else {
            java.lang.String tmp;
            syntelos.iou.chbuf strbuf = new syntelos.iou.chbuf();
            strbuf.append('(');
            java.lang.Class parameters[] = jmethod.getParameterTypes(), jclass;
            for (int idx = 0, len = parameters.length; idx < len; idx++){
                jclass = parameters[idx];
                tmp = Type4(jclass);
                strbuf.append(tmp);
            }
            strbuf.append(')');
            jclass = jmethod.getReturnType();
            tmp = Type4(jclass);
            return strbuf.toString();
        }
    }


    /**
     * <p> This {@link Type} refers to a generic descriptor string. </p>
     *
     * @see Type$Class
     * @see Field
     * @see Method
     */
    public static class Descriptor 
        extends Type 
    {
        public final static syntelos.iou.dom.Name NAME = new syntelos.iou.dom.impl.Name(ELEMENT_NODE,"http://www.syntelos.org/xb","xb:type.descriptor");

        public Descriptor(org.w3c.dom.Document doc, syntelos.iou.dom.Name name){
            super(doc,name);
        }
        public Descriptor(org.w3c.dom.Document doc){
            this(doc,NAME);
        }
        public Descriptor(){
            this(DOC_NIL);
        }
        /**
         * A complete descriptor has const
         */
        public Descriptor(CF.Constants.Utf8 utf8){
            this(DOC_NIL);
            this.setDescriptorConst(utf8);
        }

        public syntelos.iou.dom.Node decompile(){
            this.getDescriptorConst();

            return this;
        }
        public syntelos.iou.dom.Node compile(){
            this.getDescriptorConst();

            return this;
        }
        public CF.Constants.Utf8 getDescriptorConst(){
            return (CF.Constants.Utf8)this.getChildData(IO.Data.CONST_UTF8,this);
        }
        public void setDescriptorConst(CF.Constants.Utf8 utf8){
            this.setChildData(utf8,this);
        }
        public java.lang.String getDescriptorString(){
            CF.Constants.Utf8 desc = this.getDescriptorConst();
            if (null != desc)
                return desc.getString();
            else
                throw new Error.State("Missing descriptor value");
        }
        public int getDescriptor(){
            CF.Constants.Utf8 desc = this.getDescriptorConst();
            if (null != desc)
                return desc.getIndex();
            else
                throw new Error.State("Missing descriptor value");
        }
        public void setDescriptor(int index){
            CF.Constants constants = this.getConstants();
            CF.Constants.Utf8 desc = constants.getConstUtf8(index);
            this.setDescriptorConst(desc);
        }
        public void readCF(IO.Uri src, CF.Reader reader)
            throws java.io.IOException
        {
            int index = reader.readUnsignedShort();
            this.setDescriptor(index);
        }
        public void writeCF(IO.Uri dst, CF.Writer writer)
            throws java.io.IOException
        {
            int index = this.getDescriptor();
            writer.writeShort(index);
        }
        public java.lang.String toString(){
            return this.getDescriptorConst().toString();
        }
    }

    /**
     * <p> This {@link Type} is a "secondary class", subject of the
     * "top level" {@link syntelos.xb.Class} and its structures.
     * It refers to a class (which refers to a class descriptor
     * string). </p>
     *
     * @see Type$Descriptor
     * @see syntelos.xb.Class
     * @see Super
     */
    public static class Class 
        extends Type 
    {
        public final static syntelos.iou.dom.Name NAME = new syntelos.iou.dom.impl.Name(ELEMENT_NODE,"http://www.syntelos.org/xb","xb:type.class");


        private java.lang.Class jclass;

        public Class(org.w3c.dom.Document doc, syntelos.iou.dom.Name name){
            super(doc,name);
        }
        public Class(org.w3c.dom.Document doc){
            this(doc,NAME);
        }
        public Class(){
            this(DOC_NIL);
        }
        /**
         * A complete class has const
         */
        public Class(org.w3c.dom.Document doc, CF.Constants.Class tclass){
            this(doc);
            this.setTclassConst(tclass);
        }
        /**
         * An incomplete class has no const.
         */
        public Class(org.w3c.dom.Document doc, java.lang.Class jclass){
            this(doc);
            this.setJClass(jclass);
        }

        public java.lang.Class getJClass(){
            return this.jclass;
        }
        public void setJClass(java.lang.Class jclass){
            this.jclass = jclass;
        }
        public syntelos.iou.dom.Node decompile(){
            this.getTclassConst();

            return this;
        }
        public syntelos.iou.dom.Node compile(){
            this.getTclassConst();

            return this;
        }
        public CF.Constants.Class getTclassConst(){
            return (CF.Constants.Class)this.getChildData(IO.Data.CONST_CLASS,this);
        }
        public void setTclassConst(CF.Constants.Class tclass){
            this.setChildData(tclass,this);
        }
        public java.lang.String getTclassString(){
            CF.Constants.Class tclass = this.getTclassConst();
            if (null != tclass)
                return tclass.getNameString();
            else
                throw new Error.State("Missing operand");
        }
        public int getTclass(){
            CF.Constants.Class tclass = this.getTclassConst();
            if (null != tclass)
                return tclass.getIndex();
            else
                throw new Error.State("Missing operand");
        }
        public void setTclass(int index){
            CF.Constants constants = this.getConstants();
            CF.Constants.Class tclass = constants.getConstClass(index);
            this.setTclassConst(tclass);
        }
        public void readCF(IO.Uri src, CF.Reader reader)
            throws java.io.IOException
        {
            int index = reader.readUnsignedShort();
            this.setTclass(index);
        }
        public void writeCF(IO.Uri dst, CF.Writer writer)
            throws java.io.IOException
        {
            int index = this.getTclass();
            writer.writeShort(index);
        }

        public java.lang.String toString(){
            return this.getTclassConst().toString();
        }
    }


    public Type(org.w3c.dom.Document doc, syntelos.iou.dom.Name name){
        super(doc,name);
    }
    public Type(org.w3c.dom.Document doc){
        this(doc,NAME);
    }
    public Type(){
        this(DOC_NIL);
    }

}
